" Author        : $USER
" Created       : 16/12/2023
" License       : MIT
" Description   : 


let g:which_key_timeout = 30
let g:mapleader = get(g:, 'mapleader', ',')

function! s:async_keymap() abort
    return { 'name'  : '+asynctask'           , 
                \               'e'     :   [ 'AsyncTaskEdit'     , 'edit'           ],
                \               'g'     :   [ 'AsyncTaskLast'     , 'last'           ],
                \               'l'     :   [ 'AsyncTaskList'     , 'list'           ],
                \               'm'     :   [ 'AsyncTaskMacro'    , 'macro'          ],
                \               'p'     :   [ 'AsyncTaskProfile'  , 'profile'        ],
                \               'v'     :   [ 'AsyncTaskEnv'      , 'environment'    ],
                \             }
endfunction

function! s:file_keymap() abort
    return { 'name'  : '+file',
                \ 's': ['update', 'save-file'],
                \ }
endfunction

function! s:open_keymap() abort
    return { 'name'  : '+open' ,
                \ 'q': ['copen', 'open-quickfix'],
                \ 'l': ['lopen', 'open-locationlist'],
                \ }
    " nnoremap <silent> <leader>oq  :copen<CR>
    " nnoremap <silent> <leader>ol  :lopen<CR>
endfunction

function! s:buffer_keymap() abort
    return {   'name' : '+buffer' ,
                \ '1' : ['b1'           , 'buffer 1']        ,
                \ '2' : ['b2'           , 'buffer 2']        ,
                \ 'd' : ['bd'           , 'delete-buffer']   ,
                \ 'e' : ['BufExplorer'  , 'buffer-explorer'] ,
                \ 'f' : ['bfirst'       , 'first-buffer']    ,
                \ 'h' : ['Startify'     , 'home-buffer']     ,
                \ 'l' : ['blast'        , 'last-buffer']     ,
                \ 'n' : ['bnext'        , 'next-buffer']     ,
                \ 'p' : ['bprevious'    , 'previous-buffer'] ,
                \ '?' : ['Buffers'      , 'fzf-buffer']      ,
                \ }
endfunction

function! s:leaderf_keymap() abort
    return {    'name': '+leaderf',
                \               'b'    : [ 'LeaderfBuffer'              , 'buffer'          ],
                \               'c'    : [ 'LeaderfCommand'             , 'command'         ],
                \               'C'    : [ 'LeaderfColorscheme'         , 'colorscheme'     ],
                \               'f'    : [ 'LeaderfFile'                , 'file'            ],
                \               'F'    : [ 'LeaderfFunction'            , 'function'        ],
                \               'g'    : [ 'LeaderfRgInteractive'       , 'grep'            ],
                \               'h'    : [ 'LeaderfHelp'                , 'help'            ],
                \               'j'    : [ 'Leaderf jumps'              , 'jumps'           ],
                \               'l'    : [ 'LeaderfLine'                , 'line'            ],
                \               'L'    : [ 'LeaderfLocList'             , 'location'        ],
                \               'q'    : [ 'LeaderfQuickFix'            , 'quickfix'        ],
                \               'r'    : [ 'LeaderfMru'                 , 'recent'          ],
                \               's'    : [ 'LeaderfSelf'                , 'leaderf'         ],
                \               'w'    : [ 'LeaderfWindow'              , 'window'          ],
                \ }
endfunction

function! s:languange_keymap() abort 
    return {   'name' : '+lsp'                                            ,
                \ 'f' : ['LanguageClient#textDocument_formatting()'     , 'formatting'] ,
                \ 'h' : ['LanguageClient#textDocument_hover()'          , 'hover'] ,
                \ 'r' : ['LanguageClient#textDocument_references()'     , 'references'] ,
                \ 'R' : ['LanguageClient#textDocument_rename()'         , 'rename'] ,
                \ 's' : ['LanguageClient#textDocument_documentSymbol()' , 'document-symbol']  ,
                \ 'S' : ['LanguageClient#workspace_symbol()'            , 'workspace-symbol'] ,
                \ 'g' : {
                    \ 'name': '+goto',
                    \ 'd' : ['LanguageClient#textDocument_definition()'     , 'definition']       ,
                    \ 't' : ['LanguageClient#textDocument_typeDefinition()' , 'type-definition']  ,
                    \ 'i' : ['LanguageClient#textDocument_implementation()'  , 'implementation']   ,
                    \ },
                    \ }
endfunction

function! plugin#vim_which_key#load() abort
    if exists('g:loaded_vim_which_key')
        packadd vim-which-key
    endif
    return exists('g:loaded_vim_which_key')
endfunction

function! plugin#vim_which_key#keymap() abort
    let g:leader_keymap =  get(g:,'leader_keymap', { 'name'  : '+leader',
                \ 'a': s:async_keymap(),
                \ 'b': s:buffer_keymap(),
                \ 'f': s:file_keymap(),
                \ 'o': s:open_keymap(),
                \ 'l': s:leaderf_keymap(),
                \ 'L': s:languange_keymap(),
                \ 'u': plugin#unite#keymap(),
                \ })
    return g:leader_keymap
endfunction

function! plugin#vim_which_key#init() abort
    if plugin#vim_which_key#load()
        let g:mapleader = get(g:, 'mapleader', ',')
        let g:leader_keymap = plugin#vim_which_key#keymap()
        call which_key#register('<Space>', "g:leader_keymap", 'n')
        call which_key#register('<Space>', "g:leader_keymap_visual", 'v')
        nnoremap <leader> :<C-U>WhichKey '<Space>'<CR>
    endif
endfunction

